package controller;


import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import pojo.Books;
import service.BookService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@Controller
public class BookController {

    @Autowired
    @Qualifier("BookServiceImpl")
    private BookService bookService;


    @ResponseBody
    @RequestMapping(value = "/allArticle",produces="text/html;charset=utf-8")
    public String allArticle( HttpServletResponse response, HttpServletRequest request) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/json; charset=utf-8");    // 设置response的编码及格式
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Cache-Control","no-cache");
        List<Books> list = bookService.queryAllBook();
        String resJSON = JSON.toJSONString(list);     // 转换为json
        return resJSON;
    }

    @ResponseBody
    @RequestMapping(value = "/ArticleById",produces="text/html;charset=utf-8")
    public String queryAryicleById(int articleId,HttpServletResponse response, HttpServletRequest request) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/json; charset=utf-8");    // 设置response的编码及格式
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Cache-Control","no-cache");
        System.out.println(articleId);
        Books book = bookService.queryBookById(articleId);

        String resJSON = JSON.toJSONString(book);     // 转换为json
        return resJSON;
    }



    @RequestMapping("/allBook")
    public String list(Model model){
        List<Books> list = bookService.queryAllBook();
        model.addAttribute("list",list);

        return "booksManager/booksManager";
    }
    @RequestMapping("/toAddBook")
    public String toAddBook(){
        return "booksManager/AddBook";
    }
//    增加
    @RequestMapping("/addBook")
    public String addBook(Books books){
        System.out.println("增加"+books);
        bookService.addBook(books);
        return "redirect:/allBook";
    }

    @RequestMapping("/toUpdateBook")
    public String toUpdateBook(Model model,int articleId){

        Books books = bookService.queryBookById(articleId);

        model.addAttribute("book",books);
        return "booksManager/updateBook";
    }



    @RequestMapping("/updateBook")
    public String updateBook(Model model,Books book){
        System.out.println("更新"+book);
        bookService.updateBook(book);
        Books books = bookService.queryBookById(book.getArticleId());
        model.addAttribute("books", books);
        return "redirect:/allBook";
    }

    //    删除
    @RequestMapping("/delBook/{articleId}")
    public String deleteBook(@PathVariable("articleId") int articleId){
        System.out.println(articleId);
        bookService.deleteBookById(articleId);
        return "redirect:/allBook";
    }

//    查询书籍
    @RequestMapping("/queryBook")
    public String queryBookByName(String queryBookName,Model model){
        List<Books> list = bookService.queryBook(queryBookName);

        model.addAttribute("list",list);
        return "allBook";
    }

    @ResponseBody
    @RequestMapping(value = "/ArticleByName",produces="text/html;charset=utf-8")
    public String queryArticleByName(String title,HttpServletResponse response, HttpServletRequest request) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/json; charset=utf-8");    // 设置response的编码及格式
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Cache-Control","no-cache");
        List<Books> list = bookService.queryBook(title);

        String resJSON = JSON.toJSONString(list);     // 转换为json
        return resJSON;
    }
}
